package com.example.primomobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.primomobile.utils.Utils;

public class ImageAdapter extends BaseAdapter {
	private Context mContext;
	private List<ImageView> list;
	private int count = 0;
	private Integer aux;
	private boolean init = true;
	private boolean back = false;

	private HashMap<String, Object> map;

	public ImageAdapter( Context c ) {
		mContext = c;
		fill();
		init();
	}

	private void fill() {
		list = new ArrayList<ImageView>();
		for ( int i = 0; i < 16; i++ ) {
			ImageView imageView = new ImageView( mContext );
			list.add( imageView );
		}
	}

	public void init() {
		init = true;
		count = 0;
		for ( int i = 0; i < 16; i++ ) {
			addDefault( i );
		}
		notifyDataSetChanged();
		init = false;
		count = 0;
	}

	private void addDefault( int i ) {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put( Utils.PECA, Utils.DEFAULT );
		map.put( Utils.POSICAO, i );
		map.put( Utils.REMOVE, false );
		ImageView imageView = new ImageView( mContext );
		imageView.setImageResource( R.drawable.img_default );
		imageView.setTag( map );

		imageView.setOnTouchListener( new RemoverPecaListener() );
		imageView.setOnDragListener( adicionarPecaListener );
		addView( imageView );

		map.put( Utils.REMOVE, false );
		imageView.setTag( map );
	}

	OnDragListener adicionarPecaListener = new OnDragListener() {

		@Override
		public boolean onDrag( View view, DragEvent event ) {
			switch ( event.getAction() ) {
				case DragEvent.ACTION_DRAG_STARTED:
					break;
				case DragEvent.ACTION_DRAG_ENTERED:
					break;
				case DragEvent.ACTION_DRAG_EXITED:
					break;
				case DragEvent.ACTION_DROP:
					ImageView peca = (ImageView) event.getLocalState();
					Object tag = peca.getTag();
					if ( tag != null ) {
						Map pecaDestino = (Map) view.getTag();
						Map pecaOrigem = (Map) tag;

						if ( pecaOrigem.containsKey( Utils.PECA_TROCA ) && ( pecaDestino != null && (Boolean) pecaDestino.get( Utils.REMOVE ) ) ) {
							Object pecaNova = pecaOrigem.get( Utils.PECA );

							pecaOrigem.put( Utils.PECA, pecaDestino.get( Utils.PECA ) );
							pecaOrigem.put( Utils.REMOVE, true );
							pecaOrigem.put( Utils.PECA_TROCA, true );

							pecaDestino.put( Utils.PECA, pecaNova );
							if ( pecaOrigem.containsKey( Utils.POSICAO ) ) {
								Utils.getImageParaTabuleiro( (Integer) pecaOrigem.get( Utils.POSICAO ), peca );
							} else {
								Object param = pecaOrigem.get( Utils.PECA );
								if ( param.equals( Utils.DIREITA ) ) {
									peca.setImageResource( R.drawable.direita1 );
								} else if ( param.equals( Utils.ESQUERDA ) ) {
									peca.setImageResource( R.drawable.esquerda1 );
								} else if ( param.equals( Utils.FRENTE ) ) {
									peca.setImageResource( R.drawable.frente1 );
								} else if ( param.equals( Utils.FUNCAO ) ) {
									peca.setImageResource( R.drawable.funcao1 );
								}
							}
						} else {
							pecaDestino.put( Utils.PECA, pecaOrigem.get( Utils.PECA ) );
						}

						pecaDestino.put( Utils.REMOVE, true );
						pecaDestino.put( Utils.PECA_TROCA, true );
						selecionarImagem( (ImageView) view );
					}

					break;
				case DragEvent.ACTION_DRAG_ENDED:
				default:
					break;
			}
			return true;
		}
	};

	private void selecionarImagem( ImageView view ) {

		Map map = (Map) view.getTag();
		if ( map.containsKey( Utils.PECA ) ) {
			Utils.getImageParaTabuleiro( (Integer) map.get( Utils.POSICAO ), view );
		}
	}

	public void addView( ImageView view ) {

		HashMap<String, Object> hashMap = (HashMap<String, Object>) view.getTag();
		addTabuleiro( view, hashMap );

	}

	private void addTabuleiro( ImageView view, HashMap<String, Object> hashMap ) {

		if ( !init && !back ) {
			switch ( count ) {
				case 4:
					count += 3;
					back = true;
					aux = 4;
					break;
				case 12:
					count += 3;
					back = true;
					aux = 12;
					break;
				default:
					break;
			}
		}

		view = Utils.getImageParaTabuleiro( count, view );

		view.setLayoutParams( new GridView.LayoutParams( 125, 120 ) );
		view.setScaleType( ImageView.ScaleType.CENTER_CROP );
		view.setPadding( 0, 0, 0, 0 );

		hashMap.put( Utils.POSICAO, count );
		view.setTag( hashMap );

		list.set( count, view );

		if ( count == 18 ) {
			count = 18;
		} else if ( !back ) {
			count++;
		} else if ( aux != null && count == aux ) {
			if ( count != 12 ) {
				count += 4;
				back = false;
			} else {
				System.err.println( "TESTE" );
			}
		} else if ( !init ) {
			count--;
		}
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem( int position ) {
		return null;
	}

	@Override
	public long getItemId( int position ) {
		return 0;
	}

	@Override
	public View getView( int position, View convertView, ViewGroup parent ) {
		ImageView imageView;

		if ( list != null && !list.isEmpty() ) {
			return list.get( position );
		}

		return convertView;
	}

	public void clean() {
		init();
		notifyDataSetChanged();
	}

	public void removeView( ImageView view ) {

	}

	public String getPecas() {
		String tabuleiro = "";
		for ( int i = 0; i < list.size(); i += 4 ) {
			StringBuffer linha = new StringBuffer();
			for ( int j = i; j < i + 4; j++ ) {
				ImageView view = list.get( j );
				Map tag = ( (Map) view.getTag() );

				boolean remove = (Boolean) tag.get( Utils.REMOVE );
				boolean troca = (Boolean) ( tag.containsKey( Utils.PECA_TROCA ) ? tag.get( Utils.PECA_TROCA ) : false );
				if ( remove && troca ) {
					String peca = (String) tag.get( Utils.PECA );
					if ( peca.equals( Utils.FRENTE ) ) {
						linha.append( "F" );
					} else if ( peca.equals( Utils.ESQUERDA ) ) {
						linha.append( "E" );
					} else if ( peca.equals( Utils.DIREITA ) ) {
						linha.append( "D" );
					} else if ( peca.equals( Utils.FUNCAO ) ) {
						linha.append( "-" );
					}
				}
			}

			if ( ( i / 4 ) % 2 == 1 ) {
				linha = linha.reverse();
			}
			tabuleiro += linha.toString();
		}

		return tabuleiro;
	}
}